var Web_Host = window.location.host
var TokenName = getTokenName()
var Logout_Http_Url = getLogoutHttpUrl()
var Login_Http_Url = getLoginHttpUrl()

function getTokenName () {
  var $tokenName = 'beta_user_token'
  if (Web_Host.indexOf('localhost') >= 0 || Web_Host.indexOf('dev') > 0) {
    $tokenName = 'dev_user_token'
  } else if (Web_Host.indexOf('test') > 0) {
    $tokenName = 'test_user_token'
  } else if (Web_Host.indexOf('stage') > 0) {
    $tokenName = 'stage_user_token'
  }
  return $tokenName
}

function getToken () {
  var offset = document.cookie.indexOf(TokenName)
  if (offset == -1) return null

  offset += TokenName.length
  var end = document.cookie.indexOf(';', offset)
  if (end == -1) {
    end = document.cookie.length
  }
  var token = decodeURI(document.cookie.substring(offset + 1, end))
  return token
}

function setTokenCookie (token) {
  document.cookie = TokenName + '=' + token
}
function getLoginHttpUrl () {
  // 判断当前地址是否是私人聊天地址
  var url = window.location.href
  var suffix = ''
  if (url.indexOf('person') != -1) {
    suffix = '/liveview/#/person'
  } else if(url.indexOf('detail') != -1){
    var urlArr = url.split("?");
    suffix = '/liveview/#/detail?' + urlArr[1]
  }else {
    suffix = '/user/login?system_key=liveview'
  }
  var Login_Http_Url = 'https://sso.yunshanmeicai.com' + suffix
  if (Web_Host.indexOf('localhost') >= 0 || Web_Host.indexOf('dev') >= 0) {
    Login_Http_Url = 'http://sso.dev.yunshanmeicai.com' + suffix
  } else if (Web_Host.indexOf('test') > 0) {
    Login_Http_Url = 'http://sso.test.yunshanmeicai.com' + suffix
  } else if (Web_Host.indexOf('stage') > 0) Login_Http_Url = 'https://sso.stage.yunshanmeicai.com' + suffix
  return Login_Http_Url
}

function getLogoutHttpUrl () {
  var Logout_Http_Url = 'https://sso.yunshanmeicai.com/user/logout'
  if (Web_Host.indexOf('localhost') >= 0 || Web_Host.indexOf('dev') > 0) {
    Logout_Http_Url = 'http://sso.dev.yunshanmeicai.com/user/logout'
  } else if (Web_Host.indexOf('test') > 0) {
    Logout_Http_Url = 'http://sso.test.yunshanmeicai.com/user/logout'
  } else if (Web_Host.indexOf('stage') > 0) Logout_Http_Url = 'https://sso.stage.yunshanmeicai.com/user/logout'
  return Logout_Http_Url
}

function formatDate (revertime) {
  var timestamp = new Date(parseInt(revertime))
  var stampyear = timestamp.getFullYear()
  var stampmonth = timestamp.getMonth() + 1
  var stampdate = timestamp.getDate()
  var stamphour = timestamp.getHours() // 小时
  var stampminute = timestamp.getMinutes() // 分
  var stampseconds = timestamp.getSeconds()
  return stampyear + '-' + pad(stampmonth) + '-' + pad(stampdate) + ' ' + pad(stamphour) + ':' + pad(stampminute) + ':' + pad(stampseconds)
}

function formatYYYYMMDD (revertime, day) {
  var timestamp = new Date(parseInt(revertime))
  var stampyear = timestamp.getFullYear()
  var stampmonth = timestamp.getMonth() + 1
  var stampdate = timestamp.getDate() + day
  return stampyear + '-' + pad(stampmonth) + '-' + pad(stampdate)
}

function getSysDate () {
  var mydate = new Date()
  var currentYear = mydate.getFullYear()
  var currentMonth = mydate.getMonth() + 1
  var currentDate = mydate.getDate()
  return currentYear + '-' + pad(currentMonth) + '-' + pad(currentDate)
}

// currentDate格式:2017-01-01
function getAppointDate (currentDate) {
  var comp = currentDate.split('-')
  var year = comp[0]
  var month = comp[1]
  var date = comp[2]
  if (month == '01') { // 前一月跨年
    month = 12
    year = year - 1
  } else {
    month = month - 1
    if (month == 2 && date > 28) {
      date = isLeapYear(year) ? 29 : 28
    } else if (date == 31) {
      switch (month) {
        case 4:
        case 6:
        case 9:
        case 11:
          date = 30
          break
        default:
          break
      }
    }
  }
  month = ('' + month).length == 1 ? ('0' + month) : ('' + month)
  var dateFormat = year + '-' + month + '-' + date
  return dateFormat
}

// 闰年能被4整除且不能被100整除，或能被400整除
function isLeapYear (year) {
  return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)
}

function secondToHour (second) {
  return (parseInt(second) / 3600).toFixed(0)
}

function pad (date) {
  if (date < 10) {
    return '0' + date
  } else {
    return date
  }
}

function goLogin () {
  window.location.href = Login_Http_Url
}

if (window.location.host.indexOf('localhost') >= 0) {
  // 开发模式自动调用接口获取token 并写入cookie
  document.write('<script src="http://sso.dev.yunshanmeicai.com/site/tokenCookie?callback=setTokenCookie"></script>')
}

Date.prototype.Format = function (fmt) {
  var o = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'h+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    'S': this.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  }
  return fmt
}

function dateFormatShort (date) {
  if (!date) return ''
  return new Date(date).Format('yyyy-MM-dd')
}

function dateFormatLong (date) {
  if (!date) return ''
  return new Date(date).Format('yyyy-MM-dd HH:mm:ss')
}

//  数字 千分位
function toThousands (num) {
  if (num == 0) {
    return ''
  }
  var sp = num.split('.')
  num = sp[0]
  var num = (num || 0).toString()
  var result = ''
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result
    num = num.slice(0, num.length - 3)
  }
  if (num) {
    result = num + result + '.' + sp[1]
  }
  return result
}

//  数字 千分位
function toThousandsHavingZero (num) {
  var sp = num.split('.')
  num = sp[0]
  var num = (num || 0).toString()
  var result = ''
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result
    num = num.slice(0, num.length - 3)
  }
  if (num) {
    result = num + result + '.' + sp[1];
  }
  return result;
}
